package com.blb.proxy3;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Method;

public class CGLibFactoryProxy implements MethodInterceptor{

    //被代理对象
    private Object target;
    //创建代理对象
    public Object createProxyInstance(Object target){
        this.target=target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback((Callback) this);
        return enhancer.create();

    }
    //调用原有方法，进行增强
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("CGLib代理给产品打广告");
        Object result = method.invoke(target, objects);
        System.out.println("CGLib代理给产品做售后");
        return result;
    }
}
